<?php
/**
 * Collect.php
 * Author: luoji (867404364@qq.com)
 * Date: 2020-12-22
 * Time: 21:00
 */

namespace App\Http\Controllers\Tool;


use App\Exceptions\RunException;
use App\Http\Controllers\Base;
use App\Tables;

class Collect extends Base
{
    public function saveCustomer()
    {
        $customerName = $this->request->post('customerName');
        $pyCode = $this->request->post('pyCode');
        $address = $this->request->post('address');
        $dw = $this->request->post('dw');
        $contactName = $this->request->post('contactName');
        $contactPhoneNumber = $this->request->post('contactPhoneNumber');
        $qq = $this->request->post('qq');
        $wx = $this->request->post('wx');
        $remark = $this->request->post('remark');
        if(empty($customerName)) {
            return $this->responseFail('请输入客户名称');
        }
        $find = Tables\Customer::query()->where('name', $customerName)->first();
        if(!empty($find)) {
            return $this->responseFail('客户名称已存在');
        }
        if(empty($address)) {
            return $this->responseFail('请输入地址');
        }
        if(empty($dw)) {
            return $this->responseFail('请获取定位');
        }
        \DB::beginTransaction();
        try {
            $addTime = time();
            $cid = Tables\Customer::insertGetId([
                'name' => $customerName,
                'py_code' => $pyCode,
                'type' => 1,
                'coordinate' => $dw,
                'qq' => $qq ?? 0,
                'wx' => $wx ?? '',
                'remark' => $remark,
                'add_time' => $addTime
            ]);
            if($cid <= 0) { throw new RunException('增加客户表失败');}
            $ok = (bool) Tables\CustomerAddress::insert([
                'cid' => $cid,
                'name' => $address,
                'primary' => 1,
                'add_time' => $addTime
            ]);
            if(!$ok) { throw new RunException('增加客户地址表失败');}

            $ok = (bool) Tables\CustomerContacts::insert([
                'cid' => $cid,
                'name' => $contactName,
                'phone_number' => $contactPhoneNumber,
                'primary' => 1,
                'add_time' => $addTime
            ]);
            if(!$ok) { throw new RunException('增加客户联系表失败');}
            \DB::commit();
        }catch (RunException $e) {
            \DB::rollBack();
            return $this->responseFail($e->getMessage());
        }catch (\Exception  $e) {
            \DB::rollBack();
            \Log::error($e->getMessage());
            return $this->responseFail('数据库错误');
        }

        return $this->responseSuccess();
    }

    public function getCustomerList()
    {
        return $this->responseSuccess(Tables\Customer::query()->pluck('name'));
    }

    public function getPointList()
    {
        $cList = Tables\Customer::query()
            ->where('coordinate', '!=' , '')
            ->get();
        $r = [];
        foreach($cList as $c) {
            $r[] = [
                'name' => $c['name'],
                'dw'  => $c['coordinate']
            ];
        }

        return $this->responseSuccess($r);
    }

}